package com.ruoyi.apiserver.controller;

import com.ruoyi.apiserver.services.RabbitMQProducerService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/rabbitmq")
public class RabbitMQTestController {

    @Autowired
    private RabbitMQProducerService rabbitMQProducerService;

    /**
     * 发送简单字符串消息
     * @param message 消息内容
     * @return 结果
     */
    @PostMapping("/sendSimpleMessage")
    @Anonymous
    public AjaxResult sendSimpleMessage(@RequestParam String message) {
        try {
            rabbitMQProducerService.sendSimpleMessage(message);
            return AjaxResult.success("消息发送成功");
        } catch (Exception e) {
            return AjaxResult.error("消息发送失败: " + e.getMessage());
        }
    }

    /**
     * 发送Map类型消息
     * @return 结果
     */
    @PostMapping("/sendMapMessage")
    @Anonymous
    public AjaxResult sendMapMessage() {
        try {
            Map<String, Object> message = new HashMap<>();
            message.put("name", "心灵港湾");
            message.put("type", "心理健康");
            message.put("timestamp", System.currentTimeMillis());
            message.put("data", "这是一条测试消息");

            rabbitMQProducerService.sendMessageWithMap(message);
            return AjaxResult.success("Map消息发送成功");
        } catch (Exception e) {
            return AjaxResult.error("Map消息发送失败: " + e.getMessage());
        }
    }

    /**
     * 发送自定义路由键消息
     * @param routingKey 路由键
     * @param message 消息内容
     * @return 结果
     */
    @PostMapping("/sendWithRoutingKey")
    public AjaxResult sendWithRoutingKey(@RequestParam String routingKey, @RequestParam String message) {
        try {
            rabbitMQProducerService.sendMessageWithRoutingKey(routingKey, message);
            return AjaxResult.success("指定路由键消息发送成功");
        } catch (Exception e) {
            return AjaxResult.error("指定路由键消息发送失败: " + e.getMessage());
        }
    }
}